package com.example.shopping.controller;

import com.example.shopping.dao.IContentCategoryDao;
import com.example.shopping.pojo.ContentCategory;
import com.example.shopping.service.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
Create by 苍狼 于 2020/1/16
站在巅峰看世界，
落在深渊思人生。
*/
@Controller
@CrossOrigin
public class ContentCategoryController {
    @Autowired(required = false)
    private ContentCategoryService contentCategoryService;

    @RequestMapping("/selectContentCategory")
    public String selectContentCategory(HttpServletRequest request) {
        List<ContentCategory> contentCategories = contentCategoryService.selectContentCategory();
        request.getSession().setAttribute("contentCategories", contentCategories);
        return "contentCategory";
    }

    //新建广告分类
    @RequestMapping("/insertContentCategory")
    public String insertContentCategory(HttpServletRequest request, ContentCategory contentCategory) {
        int i = contentCategoryService.insertContentCategory(contentCategory);
        if (i > 0) {
            return "redirect:selectContentCategory";
        }
        return "redirect:insertContentCategory";
    }

    //删除广告分类
    @RequestMapping("/deleteContentCategoryById")
    public String deleteContentCategoryById(int id) {
        int i = contentCategoryService.deleteContentCategoryById(id);
        if (i > 0) {
            return "redirect:selectContentCategory";
        }
        return "redirect:deleteContentCategoryById";
    }

    //按照名称迷糊查询
    @RequestMapping("/selectContentCategoryByName")
    public String selectContentCategoryByName(String name, HttpServletRequest request) {
        List<ContentCategory> contentCategories = contentCategoryService.selectContentCategoryByName(name);
        request.getSession().setAttribute("contentCategories", contentCategories);
        return "category";
    }

    //修改为开启状态
    @RequestMapping("/updateContentCategoryStatusOpen")
    public String updateContentStatusOpen(int id) {
        int i = contentCategoryService.updateContentCategoryStatusOpen(id);
            return "redirect:selectContentCategory";
    }

    //修改为关闭状态
    @RequestMapping("/updateContentCategoryStatusClose")
    public String updateContentStatusClose(int id) {
        int i = contentCategoryService.updateContentCategoryStatusClose(id);
        return "redirect:selectContentCategory";
    }
}
